package com.cswangdao.controller.user;

import com.cswangdao.domain.dto.WXCheckoutDTO;
import com.cswangdao.domain.vo.WXCheckoutVo;
import com.cswangdao.domain.vo.WXCartIndexVo;
import com.cswangdao.result.Result;
import com.cswangdao.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/cart")
@Slf4j
public class WxCartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/goodscount")
    public Result<Integer> goodsCount(){
        log.info("获取购物车商品数量");
        Integer count = cartService.getCartGoodsCount();
        return Result.success(count);
    }

    @PostMapping("/add")
    public Result<Integer> add(@RequestBody Map map){
        log.info("添加购物车商品:{}",map);
        Integer count =cartService.addGoodsToCart(map);
        return Result.success(count);
    }

    @GetMapping("/index")
    public Result<WXCartIndexVo> index(){

        log.info("获取购物车列表");
        WXCartIndexVo cartIndexVo = cartService.getCartListByUserId();
        return Result.success(cartIndexVo);
    }

    @PostMapping("/checked")
    public Result<WXCartIndexVo> checked(@RequestBody Map map){
        log.info("更新购物车商品选中状态:{}",map);
        WXCartIndexVo cartIndexVo = cartService.modifyCartCheckedStatus(map);
        return Result.success(cartIndexVo);
    }

    @PostMapping("/fastadd")
    public Result<Integer> fastadd(@RequestBody Map map){
        log.info("快速添加购物车商品:{}",map);
        Integer cartId =cartService.fastAddGoodsToCart(map);
        return Result.success(cartId);
    }

    @GetMapping("/checkout")
    public Result<WXCheckoutVo> checkout(WXCheckoutDTO wxCheckoutDTO){
        log.info("获取购物车结算信息");
        WXCheckoutVo checkoutVo = cartService.getCartCheckoutVo(wxCheckoutDTO);
        return Result.success(checkoutVo);
    }

    @PostMapping("/delete")
    public Result<WXCartIndexVo> delete(@RequestBody Map map){
        log.info("删除购物车商品:{}",map);
        List<Integer> productIds = (List<Integer>) map.get("productIds");
        WXCartIndexVo wxCartIndexVo = cartService.deleteBatch(productIds);
        return Result.success(wxCartIndexVo);
    }

    @PostMapping("/update")
    public Result updateNumber(@RequestBody Map map){
        log.info("更新购物车商品数量:{}",map);
        cartService.updateProductNumber(map);
        return Result.success();
    }


}
